import BaseSprite from "../../../Script/Base/BaseSprite";
import { EResType } from "../../../Script/Common/Enum";
import { GlobalData } from "../../../Script/Common/GlobalData";
import GameUtil from "../../../Script/Util/GameUtil";
import { LoadUtil } from "../../../Script/Util/LoadUtil";
import ChatUI, { IShowItem } from "./ChatUI";

const { ccclass, property } = cc._decorator;

@ccclass
export default class ItemChat extends BaseSprite {

    @property({ type: cc.Node, tooltip: "other" })
    private other: cc.Node = null;

    @property({ type: cc.Node, tooltip: "self" })
    private self: cc.Node = null;

    public async setData(data: IShowItem) {
        this.self.active = data.name === "顾云泽";
        this.other.active = data.name !== "顾云泽";

        let node = this.self.active ? this.self : this.other;
        const icon = cc.find("icon", node).getComponent(cc.Sprite);
        const bg = cc.find("bg", node);
        const lab = cc.find("labChat", bg).getComponent(cc.Label);
        lab.string = `${data.chat}`;
        setTimeout(() => {
            node.height = bg.height + 40;
            this.node.height = node.height;
        }, 50);
        LoadUtil.instance.setSpriteFrame(icon, EResType.BubbleIcon, data.name, 90, 90);

        await GameUtil.sleep(GlobalData.isFast ? 100 : 500);
        ChatUI.instance.refreshTalk();
    }
}